package com.lg.concurrent.aqs_cas.mashibing.s3;

import java.util.concurrent.locks.ReentrantLock;

public class Demo3 {
    public static int sum = 0;
    public static ReentrantLock reentrantLock = new ReentrantLock();
    public static void main(String[] args) throws InterruptedException {

        System.out.println("当前核数： " + Runtime.getRuntime().availableProcessors());
        Thread[] threads = new Thread[Runtime.getRuntime().availableProcessors()];
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(() -> {
                reentrantLock.lock();
                try {
                    for (int j = 0; j < 100; j++)
                        sum++;
                }finally {
                    reentrantLock.unlock();
                }

            });
        }

        for (Thread thread : threads) {
            thread.start();
        }

        for (Thread thread : threads) {
            thread.join();
        }

        System.out.println(sum);

    }
}
